package com.sky.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.entity.User;
import com.sky.exception.UserNotLoginException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.mapper.UserMapper;
import com.sky.properties.WeChatProperties;
import com.sky.service.ShoppingCartService;
import com.sky.service.UserService;
import com.sky.utils.HttpClientUtil;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
//       添加购物车信息 进入 表中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

//        设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

//        判断商品在不在购物车中
//            先根据条件查询购物车
//        其实不可能是集合 userID  setmealid  或者dishid
        List<ShoppingCart> cartList= shoppingCartMapper.list(shoppingCart);



        if (cartList!=null && cartList.size()==1){
//     如果list不为空 ，且长度大于等于1 +1
            ShoppingCart shoppingCart1 = cartList.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber()+1);
            shoppingCartMapper.updateNumber(shoppingCart1);

        }else {
//           商品没有添加过购物车,直接插入即可  数量=1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            if (shoppingCart.getDishId()!=null){
//                菜品
                Dish dish = dishMapper.getById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            }else {
//                套餐

                SetmealVO setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

            }

            log.info("新增购物车：{}",shoppingCart);
           shoppingCartMapper.insert(shoppingCart);

        }




    }

    @Override
    public List<ShoppingCart> list() {

        ShoppingCart shoppingCart = new ShoppingCart();

        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void clean() {

        ShoppingCart shoppingCart = new ShoppingCart();

        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.clean(shoppingCart);
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

//        设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

//        判断商品在不在购物车中
//            先根据条件查询购物车
//        其实不可能是集合 userID  setmealid  或者dishid
        List<ShoppingCart> cartList= shoppingCartMapper.list(shoppingCart);

        if (cartList!=null && cartList.size()>0){

            ShoppingCart shoppingCart1 = cartList.get(0);
//            获得数量
            Integer number = shoppingCart1.getNumber();

            if (number>1){


                shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
                shoppingCartMapper.updateNumber(shoppingCart1);

            }else {

                //userid  setmealid  dishid
                shoppingCartMapper.clean(shoppingCart1);

            }


        }



    }
}
